package edu.zqxy.eastmoney.utils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class CrawerUtils {
//    获取目标网页内容办法
public static String getUrlContent(String urlStr){
//    http://93.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112406711461341103311_1631497796634&pn=1&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1631497796635
    String content="";
    try {
        //1.创建url
        URL url=new URL(urlStr);
        //构建连接对象
        HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
        urlConnection.setConnectTimeout(10*1000);
        urlConnection.setRequestMethod("GET");
        //模拟浏览器
        urlConnection.setRequestProperty("user-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36");
        //连接到服务器，获取数据
        urlConnection.connect();
        InputStream is=urlConnection.getInputStream();
        byte[] buff=new byte[1024];

        //读入数据
        StringBuffer sb=new StringBuffer();
        int len=0;
        while((len=is.read(buff))>0)
        {
            String str=new String(buff,0,len);
            sb.append(str);//添加进去
        }
        //得到的内容
content=sb.toString();


    } catch (Exception e) {
        e.printStackTrace();
    }
    return content;

}
}
